#! /usr/bin/python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\AFT\.pylib')

import glob, sys, os, re, string
import pepe.p_txt.bigtxt as bt
import pepe.p_string.cp1250ascii as psc

#sas variable name can not contain '/' and mus be les then 32 char long
sas_variable = lambda x_s: re.sub('_{1,}$','',upper_clean(string.strip(re.sub('/','_',x_s)[:30])))+'_'
upper_clean = lambda x_s: re.sub('\W','_',psc.uppercase(string.strip(x_s)))

def csvsas(inpfile, cdelimiter='|', hasHeader='0', headerCustom=''):
    '''
    Purpose: Create SAS data load sections from csv file.
    Syntax: csv-sas <inpfile> <cdelimiter> <has-header> <custom-output-header>
    Example: csv-sas gl_.dat \"|\" 1 \"\"
    '''
    #print string.join([inpfile, cdelimiter, hasHeader, headerCustom], ';')
    out_fl = open('#csv-sas.out','w')
    #for inf_s in glob.glob(inpfiles):
    out_h = {}
    flcnt_n = 0
    header_l = []
    for line_s in bt.lines(inpfile):
        flcnt_n += 1
        #print line_s
        #print cdelimiter
        columns_l = string.split(line_s[:-1], cdelimiter)
        for index, value in enumerate(columns_l):
            cindex = 'c'+string.zfill(index+1,3)
            if  hasHeader == "1" and flcnt_n == 1:
                header_l = columns_l
            elif hasHeader == "0" and headerCustom <> '' and flcnt_n == 1:
                header_l = string.split(headerCustom,cdelimiter)
            else:
                if out_h.get(cindex):
                    if  out_h[cindex][0] < len(str(value)):
                        out_h[cindex][0] = len(str(value))
                else:
                    if len(header_l) > 1:
                        out_h[cindex] = [len(str(value)),header_l[index], value]
                    else:
                        out_h[cindex] = [len(str(value)),'',value]

    template_s = """        %orig_name%     FORMAT = $%col_len%."""
    for i in sorted(out_h.keys()):
        
        out_s = re.sub('%orig_name%'    , string.ljust(sas_variable(out_h[i][1]),45), template_s)
        if out_h[i][0] == 0: out_s = re.sub('%col_len%'      ,'1', out_s)
        else: out_s = re.sub('%col_len%'      , str(out_h[i][0]), out_s)
        out_fl.write(out_s + '\n')
        out_s = ''

    template_s = """        %orig_name%     = SCAN(in,%col_no%,'%cdelimiter%');"""
    for i in sorted(out_h.keys()):
        out_s = re.sub('%orig_name%'    , string.ljust(sas_variable(out_h[i][1]),45), template_s)
        out_s = re.sub('%col_no%'      , str(int(i[1:])), out_s)
        out_s = re.sub('%cdelimiter%'  , cdelimiter, out_s)
        out_fl.write(out_s + '\n')
        out_s = ''

    template_s = """ %orig_name%"""
    out_s = ''
    for i in sorted(out_h.keys()):
        out_s += ' ' + sas_variable(out_h[i][1])+ '\n'
    out_fl.write('drop ' + out_s + ';\n')
    out_s = ''
        
    template_s = """ %orig_name% -- %orig_value%"""
    for i in sorted(out_h.keys()):
        out_s = re.sub('%orig_name%'    , string.ljust(sas_variable(out_h[i][1]),45), template_s)
        out_s = re.sub('%orig_value%'   , str(out_h[i][2]), out_s)
        out_fl.write(out_s + '\n')
        out_s = ''

    out_fl.close()
    return 1

if __name__ == "__main__":
    #csvsas('gl_.dat', '|', "1", "")
    #print sys.argv
    #print os.getcwd()  os.getcwd()+'\\'+
    if len(sys.argv) != 5:
        print csvsas.__doc__
    else:
        csvsas(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

